/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.chushizhang;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.ChushiZhangRight;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.model.JingliCaiGouDanModel;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class MychuShiRightAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<ChushiZhangRight> mGroupList;
    Context context;
    ButtomDialogView bottomSheetDialog;
    private int checkedPosition = -1;

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void onItemClickListener(View view, int pos, List<Group> groupList);
    }

    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition_parentposition;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public MychuShiRightAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;

    }

    public void setCheckedPosition(int checkedPosition, int checkedPosition_parentposition) {
        this.checkedPosition = checkedPosition;
        this.checkedPosition_parentposition = checkedPosition_parentposition;
        notifyDataSetChanged();
    }

    public void setGroupList(List<ChushiZhangRight> groupList) {
        this.mGroupList = groupList;
    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int childItemCount(int parentPosition) {
        List<ChushiZhangRight.DetailsBean> memberList = mGroupList.get(parentPosition).getDetails();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_cchushir_ight_expand_parent, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_test, root, false);

        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
//        View view = mInflater.inflate(R.layout.item_chushi_shenhe_child, root, false);
        View view = mInflater.inflate(R.layout.item_chushi_right_expand, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_child, root, false);
        return new ChildHolder(view, this);
    }

    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        ChushiZhangRight data = mGroupList.get(position);
        ParentHolder parentHolder = (ParentHolder) holder;
        parentHolder.mTvTitle.setText(data.getApplyUser());
        Log.d("ISEXPAndedisexpanded", data.isExpanded() + "");
//        layout_parent
        parentHolder.mIvStatus.setSelected(data.isExpanded());
        parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_item2));
        Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.arrow_right_black)).getBitmap();
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (data.isExpanded() == true) {
            parentHolder.mIvStatus.setImageBitmap(bitmap);
        } else {
            parentHolder.mIvStatus.setImageResource(R.mipmap.arrow_right_black);
            parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem1));
        }
        parentHolder.text_time.setText(data.getLimitDate());
/*        if (data.getDetails().get(0).getState().equals("3")) {
            parentHolder.text_status.setText("已收货");
        } else if (data.getDetails().get(0).equals("0")) {
            parentHolder.text_status.setText("采购中");
        }else if (data.getDetails().get(0).getState().equals("1")){
            parentHolder.text_status.setText("已审核");
        }else if (data.getDetails().get(0).getState().equals("2")){
            parentHolder.text_status.setText("已驳回");
        }*/
        if (data.getDetails().get(0).getState().equals("9")) {
            parentHolder.text_status.setText("已收货");
        } else{
            parentHolder.text_status.setText("采购中");
        }
    }

    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder)holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        ChildHolder myholder = (ChildHolder) holder;
//        myholder.setData(mGroupList.get(parentPosition).getMemberList().get(position));
        mGroupList.get(parentPosition).isExpanded();
        int childsize = mGroupList.get(parentPosition).getDetails().size();
        ChushiZhangRight.DetailsBean applyListBean = mGroupList.get(parentPosition).getDetails().get(position);
//        shape_chushi_dingdan_ziitem3
        if (position == childsize - 1) {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem2));
        } else {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        }
//        myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        ChushiZhangRight.DetailsBean data = mGroupList.get(parentPosition).getDetails().get(position);
        myholder.mTvTitle.setText(data.getCommodityName());
        myholder.text_name.setText(mGroupList.get(parentPosition).getDetails().get(position).getCommodityName());
//        myholder.text_count.setText(mGroupList.get(parentPosition).getDetails().get(position).getTransitAmount() + "");
        myholder.text_mark.setText(mGroupList.get(parentPosition).getDetails().get(position).getMark() + "");
        myholder.text_guige.setText("规格: " + mGroupList.get(parentPosition).getDetails().get(position).getSpecs());


        DecimalFormat format = new DecimalFormat("");
        String str = format.format(mGroupList.get(parentPosition).getDetails().get(position).getApplyAmount());
        myholder.applyAmount.setText("数量: " + str + "");
        Glide.with(context).load(AppConfig.IMGURL + mGroupList.get(parentPosition).getDetails().get(position).getPhoto()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(myholder.ivAvatar);

        if (position == checkedPosition && checkedPosition_parentposition == parentPosition) {
            myholder.view_select.setVisibility(View.VISIBLE);

        } else {
            myholder.view_select.setVisibility(View.GONE);
        }
    /*    if (applyListBean.getState().equals("0")) {
            myholder.layout_item.setBackgroundColor(Color.parseColor("#FFF3F3"));

//            childHolder.view_select.setVisibility(View.GONE);
            myholder.view_select.setVisibility(View.GONE);
        } else {
            myholder.layout_item.setBackgroundColor(Color.parseColor("#FFFFFF"));
            myholder.view_select.setVisibility(View.GONE);
        }
        if (position == checkedPosition && checkedPosition_parentposition == parentPosition) {
            myholder.view_select.setVisibility(View.VISIBLE);

        } else {
            myholder.view_select.setVisibility(View.GONE);
        }
*/

    }

    static class ParentHolder extends ViewHolder {

        TextView mTvTitle;
        ImageView mIvStatus;
        RelativeLayout layout_parent;
        TextView text_time;
        TextView text_status;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mIvStatus = itemView.findViewById(R.id.iv_status);
            layout_parent = itemView.findViewById(R.id.layout_parent);
            text_time = itemView.findViewById(R.id.text_time);
            text_status = itemView.findViewById(R.id.text_status);
        }

        public void setData(Group data) {
            mTvTitle.setText(data.getName());
            mIvStatus.setSelected(data.isExpanded());
        }
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;

        LinearLayout layout_item;
        TextView text_name;
        TextView applyAmount;
        TextView text_mark;
        TextView text_guige;
        ImageView ivAvatar;
        View view_select;

        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);

            layout_item = itemView.findViewById(R.id.layout_item);
            text_name = itemView.findViewById(R.id.text_name);
            text_mark = itemView.findViewById(R.id.text_mark);
            text_guige = itemView.findViewById(R.id.text_guige);
            applyAmount = itemView.findViewById(R.id.applyAmount);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            view_select = itemView.findViewById(R.id.view_select);
        }

        public void setData(GroupMember data) {
            mTvTitle.setText(data.getName());
        }
    }


}